capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure IV 
*/

local data "" ;
local output "" ;

foreach gp in all male female { ;
	use "prwora_bw264_crime_poly1allcov_iv_`gp'.dta", clear ;
	keep if 
		(
		variables == "n_crime2char_1997_2017" | 
		variables == "n_crime2chart_1997_2017" |
		variables == "n_crime2genr_1997_2017"	| 
		variables == "n_crime2genrt_1997_2017"	|
		variables == "n_crime2nogn_1997_2017" | 
		variables == "n_crime2nognt_1997_2017" | 
		variables == "n_crime2prtf_1997_2017" | 
		variables == "n_crime2prtft_1997_2017" | 
		variables == "n_crime2vrob_1997_2017" | 
		variables == "n_crime2vrobt_1997_2017" | 
		variables == "n_crime2vnrb_1997_2017" | 
		variables == "n_crime2vnrbt_1997_2017" | 
		variables == "n_crime2anti_1997_2017" | 
		variables == "n_crime2antit_1997_2017" | 
		variables == "n_crime2vcps_1997_2017" | 
		variables == "n_crime2vcpst_1997_2017" | 
		variables == "n_crime2prbg_1997_2017" | 
		variables == "n_crime2prbgt_1997_2017" | 
		variables == "n_crime2dgsl_1997_2017" | 
		variables == "n_crime2dgslt_1997_2017" | 
		variables == "n_crime2prfd_1997_2017" | 
		variables == "n_crime2prfdt_1997_2017" | 
		variables == "n_crime2niwp_1997_2017" | 
		variables == "n_crime2niwpt_1997_2017" | 
		variables == "n_crime2hinc_1997_2017" |
		variables == "n_crime2shinc_1997_2017" | 
		variables == "n_crime2ahinc_1997_2017" | 
		variables == "avg_kidemp15_1997_2017")
		& sample == "age18unfav" ;
	foreach stub in 
		char chart genr genrt nogn nognt prtf prtft vrob vrobt vnrb vnrbt anti antit
		vcps vcpst prbg prbgt dgsl dgslt prfd prfdt niwp niwpt hinc shinc ahinc { ;
			replace variables = "age18rd_`stub'"   if variables == "n_crime2`stub'_1997_2017" ;
	} ;
		replace variables = "age18rd_kidemp15" if variables == "avg_kidemp15_1997_2017" ;
	rename n_age18unfav b_endo ;
	rename sen_age18unfav se_endo ;
	keep variables b_endo se_endo mean_rd ;
	gen gp = "`gp'" ;
	gen exp = "age18rd" ;
	tempfile `gp' ;
	save ``gp'' ;
} ;

/* Sex */
local sex1 "female" ;
local sex2 "male" ;
local sex1_label "Female" ;
local sex2_label "Male" ;
local sex1_color "pink" ;
local sex2_color "blue" ;

use `all', clear ;
append using ``sex1'' ;
append using ``sex2'' ;

gen treat_lvl = mean_rd + b_endo ;

gen lo95 = treat_lvl - 1.96*se_endo ;
gen hi95 = treat_lvl + 1.96*se_endo ;



capture drop sort ;
gen sort = . ;

local add_all     	= 0 ;
local add_male 		= .3 ;
local add_female    = -.3 ;

foreach gp in all male female { ;

	replace	sort = 12 + `add_`gp''  if variables == "age18rd_prtft" 	& gp == "`gp'" ;
	replace	sort = 10 + `add_`gp''  if variables == "age18rd_vcpst" 	& gp == "`gp'" ;
	replace	sort = 8  + `add_`gp''  if variables == "age18rd_prfdt" 	& gp == "`gp'" ;
	replace	sort = 6  + `add_`gp''  if variables == "age18rd_vrobt" 	& gp == "`gp'" ;
	replace	sort = 4  + `add_`gp''  if variables == "age18rd_prbgt" 	& gp == "`gp'" ;
	replace	sort = 2  + `add_`gp''  if variables == "age18rd_dgslt" 	& gp == "`gp'" ;
} ;


twoway 

		(rbar hi95 lo95	sort 	if variables == "age18rd_prtft" & gp == "all", color(gray*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vrobt" & gp == "all", color(gray*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vcpst" & gp == "all", color(gray*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prbgt" & gp == "all", color(gray*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_dgslt" & gp == "all", color(gray*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prfdt" & gp == "all", color(gray*.25) barwidth(.2) horizontal)
		
		(rbar hi95 lo95	sort 	if variables == "age18rd_prtft" & gp == "male", color(blue*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vrobt" & gp == "male", color(blue*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vcpst" & gp == "male", color(blue*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prbgt" & gp == "male", color(blue*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_dgslt" & gp == "male", color(blue*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prfdt" & gp == "male", color(blue*.25) barwidth(.2) horizontal)

		(rbar hi95 lo95	sort 	if variables == "age18rd_prtft" & gp == "female", color(pink*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vrobt" & gp == "female", color(pink*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_vcpst" & gp == "female", color(pink*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prbgt" & gp == "female", color(pink*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_dgslt" & gp == "female", color(pink*.25) barwidth(.2) horizontal)
		(rbar hi95 lo95	sort 	if variables == "age18rd_prfdt" & gp == "female", color(pink*.25) barwidth(.2) horizontal)
		
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prtft" & gp == "all", color(gray) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vrobt" & gp == "all", color(gray) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vcpst" & gp == "all", color(gray) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prbgt" & gp == "all", color(gray) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_dgslt" & gp == "all", color(gray) lwidth(medthin))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prfdt" & gp == "all", color(gray) lwidth(medthin))
		
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prtft" & gp == "male", color(blue) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vrobt" & gp == "male", color(blue) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vcpst" & gp == "male", color(blue) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prbgt" & gp == "male", color(blue) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_dgslt" & gp == "male", color(blue) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prfdt" & gp == "male", color(blue) lwidth(medthick))
		
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prtft" & gp == "female", color(pink) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vrobt" & gp == "female", color(pink) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_vcpst" & gp == "female", color(pink) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prbgt" & gp == "female", color(pink) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_dgslt" & gp == "female", color(pink) lwidth(medthick))
		(pcarrow sort mean_rd sort treat_lvl 	if variables == "age18rd_prfdt" & gp == "female", color(pink) lwidth(medthick))
		
		(scatter sort mean_rd  	if variables == "age18rd_prtft" & gp == "all", color(gray) msymbol(o) msize(small))
		(scatter sort mean_rd  	if variables == "age18rd_vrobt" & gp == "all", color(gray) msymbol(o) msize(small))
		(scatter sort mean_rd  	if variables == "age18rd_vcpst" & gp == "all", color(gray) msymbol(o) msize(small))
		(scatter sort mean_rd  	if variables == "age18rd_prbgt" & gp == "all", color(gray) msymbol(o) msize(small))
		(scatter sort mean_rd  	if variables == "age18rd_dgslt" & gp == "all", color(gray) msymbol(o) msize(small))
		(scatter sort mean_rd  	if variables == "age18rd_prfdt" & gp == "all", color(gray) msymbol(o) msize(small))
		
		(scatter sort mean_rd 	if variables == "age18rd_prtft" & gp == "male", color(blue) msymbol(Oh))
		(scatter sort mean_rd 	if variables == "age18rd_vrobt" & gp == "male", color(blue) msymbol(Oh))
		(scatter sort mean_rd 	if variables == "age18rd_vcpst" & gp == "male", color(blue) msymbol(Oh))
		(scatter sort mean_rd 	if variables == "age18rd_prbgt" & gp == "male", color(blue) msymbol(Oh))
		(scatter sort mean_rd 	if variables == "age18rd_dgslt" & gp == "male", color(blue) msymbol(Oh))
		(scatter sort mean_rd 	if variables == "age18rd_prfdt" & gp == "male", color(blue) msymbol(Oh))
		
		(scatter sort mean_rd	if variables == "age18rd_prtft" & gp == "female", color(pink) msymbol(X) msize(medlarge))
		(scatter sort mean_rd	if variables == "age18rd_vrobt" & gp == "female", color(pink) msymbol(X) msize(medlarge))
		(scatter sort mean_rd	if variables == "age18rd_vcpst" & gp == "female", color(pink) msymbol(X) msize(medlarge))
		(scatter sort mean_rd	if variables == "age18rd_prbgt" & gp == "female", color(pink) msymbol(X) msize(medlarge))
		(scatter sort mean_rd 	if variables == "age18rd_dgslt" & gp == "female", color(pink) msymbol(X) msize(medlarge))
		(scatter sort mean_rd	if variables == "age18rd_prfdt" & gp == "female", color(pink) msymbol(X) msize(medlarge))
		
		(scatter sort mean_rd if var == "kidem", lcolor(none) lwidth(medthick) yaxis(2))
		
		(pcarrowi 12.85 .275 12.5  .3  "", 	mstyle(none) lwidth(vthin) color(black)) /* For control mean label */
		(pcarrowi 12.5  .45 12.9  .5  "", 	mstyle(none) lwidth(vthin) color(black)) /* for IV estimate label */
		
		, 
		legend(col(3) row(1) order(37 "All" 43 "Male" 49 "Female" ))
		xsc(r(-.2 1)) xtitle("Increase in number of charges", size(medsmall))
		xlab(-.2(.2)1)
		ysc(r(1.5 13) axis(1)) 
		ysc(r(1.5 13) axis(2)) 
		ytitle("", axis(1)) 
		ytitle("", axis(2))
		ylabel(
		   12 "Theft" 10 "Prostitution" 8 "Fraud/forgery" 6 "Robbery" 4 "Burglary" 2 "Drug distrib", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(1) ) 
		ylabel(
		   12 "Theft" 10 "Prostitution" 8 "Fraud/forgery" 6 "Robbery" 4 "Burglary" 2 "Drug distrib", 
		   angle(0) notick labsize(small) /*nogrid*/ axis(2) ) 
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0) axis(1))
		ylabel(,nogrid angle(0) axis(2))
		ttext(13 .25 "Control" "mean", size(tiny))
		ttext(13 .54 "IV estimate", size(tiny))
		plotregion(margin(0 2 2 2))  /* get x-axis to start at 0 */
		xline(0, lcolor(gray) lpattern(dash))
		;
		graph export "`output'/fig4.png", width(1000) height(750) replace ;
		
